﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Frogger
{
    class Lane : MoveableEntity
    {
	 /*********************************************/
        public enum LaneState { Normal, Stop, SpeedUp };
        public LaneState __currentLaneState = LaneState.Normal;
        public int __stopCount = 0;
        /***************************************************/    
        //A list which contains all the cars
        List<Automobile> __carList = new List<Automobile>();
        
        public Lane(Texture2D textureImage, Vector2 position, Point frameSize,
            int collisionOffset, Point currentFrame, Point sheetSize, Vector2 speed,
            string name, int scoreValue)
            : base(textureImage, position, frameSize, collisionOffset, currentFrame,
                sheetSize, speed, name, scoreValue) {}

        public void Update(GameTime gameTime, Rectangle clientBounds, Frog player )
        {
            this.__position += this.__speed;

            if (player.GetPosition.Y < (clientBounds.Height / 3) * 2)
            {
                if (player.isJumping())
                    this.__speed.Y = -player.GetSpeed().Y;
                else
                    this.__speed = Vector2.Zero;
            }

            UpdateCars();
            base.Update(gameTime,clientBounds);
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            base.Draw(gameTime, spriteBatch);
        }

        public bool CheckOutOfBound(Rectangle clientBounds)
        {
            if (__position.Y - __frameSize.Y > clientBounds.Height)
            {
                //clear the car list,remove all cars
                for (int i = 0; i < __carList.Count; i++)
                {
                    __carList.RemoveAt(i);
                    --i;
                }
                return true;
            }
            else
                return false;
        }

       /**************************Modify*****************************************/
        public void UpdateCars(GameTime gameTime,Rectangle clientBounds)
        {

            switch(__currentLaneState)
            {
                case LaneState.Normal:
                    {
                        for (int i = 0; i < __carList.Count; i++)
                        {
                            __carList[i].direction.X = 0;
                        }
                        break;
                    }
                case LaneState.Stop:
                    {
                        __stopCount++;
                        if (__stopCount > Constants.CAR_STOP_TIME)
                        {
                            __currentLaneState = LaneState.Normal;
                            __carList[i].ResetSpeed();
                            __stopCount = 0;
                        }
                        break;
                    }
                case LaneState.SpeedUp:
                    {
                        break;
                    }
            }

            for (int i = 0; i < __carList.Count; i++)
            {
                __carList[i].direction.Y = this.direction.Y;
                __carList[i].Update(gameTime, clientBounds);
            }
        }
/*************************************************************************/
        
        //public void CreateCar( GameTime gameTime )
        //{

        //    if (gameTime.TotalGameTime.TotalMilliseconds - this.__lastSpawnTime > this.__nextSprite)
        //    {

        //        // set the current time as the last spawn time for next invokation
        //        this.__lastSpawnTime = gameTime.TotalGameTime.TotalMilliseconds;

        //        /*Create two temporary arrays of string to hold current Staff*/
        //        string[] left = new string[Constants.NOTE_NUMBER];
        //        string[] right = new string[Constants.NOTE_NUMBER];

        //        if (currentSpriteIndex < this.__scriptsCache.Count)
        //        {

        //            //read one line from script  

        //            string line = (string)this.__scriptsCache.GetByIndex(currentSpriteIndex);

        //            //get the whole line by seperating them into individual strings
        //            string[] tmp = line.Split(' ');

        //            for (int i = 0; i < Constants.NOTE_NUMBER; i++)
        //            {
        //                //read the first string as left script for sprite
        //                left[i] = tmp[0].Substring(i, 1);

        //                //read the second string as right script for sprite
        //                right[i] = tmp[1].Substring(i, 1);
        //            }

        //            //If next time is x, then set nextSprite to default
        //            if (line.IndexOf('x') > 0)
        //            {
        //                this.__nextSprite = Constants.METRONOME;
        //            }
        //            else
        //            {
        //                this.__nextSprite = Convert.ToInt32(tmp[2]);
        //            }

        //            //Accumulate the index
        //            currentSpriteIndex++;

        //            //Invoke Spawn for both left and right sides
        //            for (int i = 0; i < Constants.NOTE_NUMBER; i++)
        //            {
        //                if (left[i].Equals("1"))
        //                {
        //                    //Spawn("left", i, this.__nextSprite);
        //                    Spawn();
        //                }
        //                if (right[i].Equals("1"))
        //                {
        //                    //Spawn("right", i, this.__nextSprite);
        //                    Spawn();
        //                }
        //            }
        //        }//end if
        //    }//end if
        //}//end createCar


    }//end Class
}
